/**
 * 
 */
package com.threedt.nova.portfolio.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.threedt.nova.portfolio.dao.GenericDao;
import com.threedt.nova.portfolio.domain.User;

/**
 * @author vivek
 * 
 */
@Service(value = "userServices")
public class UserServicesImpl implements UserServices {

	@Autowired(required = true)
	private GenericDao<User,String> genericDao;

	@Override
	public Boolean deleteUsers(User user) throws ServiceException {
		return false;
	}

	@Override
	public List<User> fetchAllUsers() throws ServiceException {

		List<User> users = genericDao.findAll(User.class);

		return users;
	}

	@Override
	public User fetchUser(User user) throws ServiceException {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userName", user.getUserName());

		List<User> users =  genericDao.findAll(User.class);
		if (users != null) {
			System.out.println("The user size is " + users.size());

			for (User tempUser : users) {
				if (tempUser.getUserName().equals(user.getUserName())&& tempUser.getPassword().equals(user.getPassword())) {
					return tempUser;
				}
			}
		}

		return null;
	}

	@Override
	public User saveOrUpdateUsers(User user) throws ServiceException {
		 genericDao.merge(user);
		 return user;
		
	}

}
